package com.brokendust.client.api.module.item;

import com.brokendust.client.PixelEngine;
import com.brokendust.client.api.module.GUIComponent;
import com.brokendust.client.api.module.PullPanelComponent;
import com.brokendust.client.api.util.BasePosition;
import com.brokendust.client.config.module.ComponentJson;
import com.brokendust.client.config.module.item.ItemShowJson;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.lwjgl.opengl.GL11;

public class ItemShow extends ItemTool implements GUIComponent, PullPanelComponent {
    public int guiWidth;
    public int guiHeight;
    public int guiDWidth;
    public int guiDHeight;
    public int objectWidth;
    public int objectHeight;
    public int x;
    public int y;
    public int position;
    public String object_id;
    ItemStack itemStack;
    float scale;

    public ItemShow(ItemShowJson itemShowJson, int guiWidth, int guiHeight, int guiDWidth, int guiDHeight) {
        this.guiWidth = guiWidth;
        this.guiHeight = guiHeight;
        this.guiDWidth = guiDWidth;
        this.guiDHeight = guiDHeight;
        this.itemStack = itemShowJson.getItemStack();
        if (this.itemStack == null || this.itemStack.getItem() == Item.byId(0)) {
            this.itemStack = new ItemStack(Item.byId(1));
        }
        this.object_id = itemShowJson.getObject_name();
        this.scale = itemShowJson.getScale();
        this.position = itemShowJson.getPosition();
        this.x = itemShowJson.getX();
        this.y = itemShowJson.getY();
        this.objectWidth = (int) (16.0f * this.scale);
        this.objectHeight = (int) (16.0f * this.scale);
    }

    public ItemShow(ItemShowJson itemShowJson) {
        this.itemStack = itemShowJson.getItemStack();
        if (this.itemStack == null || this.itemStack.getItem() == Item.byId(0)) {
            this.itemStack = new ItemStack(Item.byId(1));
        }
        this.object_id = itemShowJson.getObject_name();
        this.scale = itemShowJson.getScale();
        this.position = itemShowJson.getPosition();
        this.x = itemShowJson.getX();
        this.y = itemShowJson.getY();
        this.objectWidth = (int) (16.0f * this.scale);
        this.objectHeight = (int) (16.0f * this.scale);
    }

    @Override
    public void upData(ComponentJson componentJson) {
        if (!(componentJson instanceof ItemShowJson)) {
            return;
        }
        ItemShowJson itemShowJson = (ItemShowJson) componentJson;
        this.itemStack = itemShowJson.getItemStack();
        if (this.itemStack == null || this.itemStack.getItem() == Item.byId(0)) {
            this.itemStack = new ItemStack(Item.byId(1));
        }
        this.object_id = itemShowJson.getObject_name();
        this.scale = itemShowJson.getScale();
        this.position = itemShowJson.getPosition();
        this.x = itemShowJson.getX();
        this.y = itemShowJson.getY();
        this.objectWidth = (int) (16.0f * this.scale);
        this.objectHeight = (int) (16.0f * this.scale);
    }

    @Override
    public void renderGUI(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        GlStateManager._pushMatrix();
        GlStateManager._scalef(this.scale, this.scale, this.scale);
        float mScale = (float) Math.pow(this.scale, -1.0);
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        int mmX = Math.round(mX / this.scale);
        int mmY = Math.round(mY / this.scale);
        PixelEngine.itemRenderer.renderGuiItem(this.itemStack, mmX, mmY);
        GlStateManager._scalef(mScale, mScale, mScale);
        GlStateManager._popMatrix();
        boolean switch_status = mouseX >= mX && mouseY >= mY && mouseX < mX + this.objectWidth && mouseY < mY + this.objectHeight;
        if (switch_status) {
            this.renderTooltip(this.itemStack, (int) (mX + this.objectWidth * 1.2), mY);
        }
    }

    @Override
    public void renderPull(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY, int displayX, int displayY) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        GlStateManager._pushMatrix();
        GlStateManager._scalef(this.scale, this.scale, this.scale);
        float mScale = (float) Math.pow(this.scale, -1.0);
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        int mmX = Math.round(mX / this.scale);
        int mmY = Math.round(mY / this.scale);
        double scale = Minecraft.getInstance().getWindow().getGuiScale();
        GlStateManager._blendFuncSeparate((int) (displayX * scale), (int) (displayY * scale), (int) (this.guiDWidth * scale), (int) (this.guiDHeight * scale));
        GL11.glEnable(3089);
        PixelEngine.itemRenderer.renderGuiItem(this.itemStack, mmX, mmY);
        GL11.glDisable(3089);
        GlStateManager._scalef(mScale, mScale, mScale);
        GlStateManager._popMatrix();
        int mXX = mX + this.objectWidth;
        int mYY = mY + this.objectHeight;
        int ddX = displayX + this.guiDWidth;
        int ddY = displayY + this.guiDHeight;
        boolean withinRange = mX >= displayX - this.objectWidth && mX <= ddX && mXX >= displayX && mXX <= ddX + this.objectWidth;
        boolean withinRange2 = mY >= displayY - this.objectHeight && mY <= ddY && mYY >= displayY && mYY <= ddY + this.objectHeight;
        if (withinRange && withinRange2) {
            int cutLX = 0;
            if (mX < displayX) {
                cutLX = displayX - mX;
            }
            int cutRX = 0;
            if (mXX > ddX) {
                cutRX = mXX - ddX;
            }
            int cutTY = 0;
            if (mY < displayY) {
                cutTY = displayY - mY;
            }
            int cutDY = 0;
            if (mYY > ddY) {
                cutDY = mYY - ddY;
            }
            boolean switch_status = mouseX >= mX + cutLX && mouseY >= mY + cutTY && mouseX < mX + this.objectWidth - cutRX && mouseY < mY + this.objectHeight - cutDY;
            if (switch_status) {
                this.renderTooltip(this.itemStack, (int) (mX + this.objectWidth * 1.2), mY);
            }
        }
    }

    public void renderHUD(int window_width, int window_height) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        GlStateManager._pushMatrix();
        GlStateManager._scalef(this.scale, this.scale, this.scale);
        float mScale = (float) Math.pow(this.scale, -1.0);
        Integer[] integers = BasePosition.getObjectHubPoint(window_width, window_height, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        int mmX = Math.round(mX / this.scale);
        int mmY = Math.round(mY / this.scale);
        PixelEngine.itemRenderer.renderGuiItem(this.itemStack, mmX, mmY);
        GlStateManager._scalef(mScale, mScale, mScale);
        GlStateManager._popMatrix();
    }
}

